import { getBaseUrl } from '@/services/urlconst';
import { getUserID, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';

const baseUrl = getBaseUrl();

/**
 * 获取详情
 * @param callback  获取详情回调
 * @returns
 */
const getNeedByIDService = (callback: (res: any) => void) => {
  const getNeedByID = useRequest(
    (params: any) => {
      let reqUrl = `${baseUrl}/purchaseNeedController/getPurchaseNeedByID/${params.id}/${getUserID()}`;
      return requestFunComponFiex('GET', reqUrl, {});
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getNeedByIDRun: getNeedByID?.run,
    getNeedByIDLoading: getNeedByID?.loading,
    getNeedByIDInfo: getNeedByID?.data?.obj,
    reloadDetailInfo: getNeedByID?.refresh,
  };
};

/**
 * 需求撤销
 * @param callback 接口回调
 * @returns
 */
const getNeedinvalidService = (callback: (res: any) => void) => {
  const getNeedinvalid = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchaseNeedController/invalid`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    getNeedinvalidRun: getNeedinvalid?.run,
    getNeedinvalidLoading: getNeedinvalid?.loading,
  };
};

/**
 * 查看当前流程进度
 * @controller 业务controller
 * @returns
 */
const getImageByProcInstIDService = (callBack: (res: any) => void) => {
  const getImageByProcInstID = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/purchaseNeedController/getImageByProcInstID/${params?.procInstID}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: callBack,
    },
  );

  const data = useMemo(() => {
    return getImageByProcInstID?.data;
  }, [getImageByProcInstID?.data]);

  return {
    getImageByProcInstIDRun: getImageByProcInstID?.run,
    getImageByProcInstIDLoading: getImageByProcInstID?.loading,
    getImageByProcInstIDData: data,
  };
};

/**
 * 物料详情
 * */
const getMaterialDetailServices = (Callback: (res: Record<string, any>) => void) => {
  const getMaterialDetail = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/materialController/details/${bodyParams?.id}`;
      return requestFunComponFiex('GET', reqUrl, {});
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  const data = useMemo(() => {
    return getMaterialDetail?.data?.obj;
  }, [getMaterialDetail?.data?.obj]);

  return {
    getMaterialDetailRun: getMaterialDetail?.run,
    getMaterialDetailLoading: getMaterialDetail?.loading,
    getMaterialDetailData: data,
  };
};

export { getNeedByIDService, getNeedinvalidService, getImageByProcInstIDService, getMaterialDetailServices };
